/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import justdoit.utility.Hash;

/**
 * The Entity Class for a User
 * @author Can
 */
public class User {

    private String id;
    private String userName;
    private String passwordHashValue;
    private Locale locale;
    private List<Task> tasks;
    private Theme theme;

    public User(String userId, String userName, Locale locale) {
        this.id = userId;
        this.userName = userName;
        this.locale = locale;
        this.theme = new Theme(Theme.METAL);
        tasks = new ArrayList<Task>();
    }

    public User(String userName, Locale locale) {
        this.id = String.valueOf(System.currentTimeMillis() / 1000);
        this.userName = userName;
        this.locale = locale;
        this.theme = new Theme(Theme.METAL);
        tasks = new ArrayList<Task>();
    }

    public Color getColor() {
        return this.theme.getColor();
    }

    public void setColor(Color color) {
        this.theme.setColor(color);
    }

    public String getTheme() {
        return theme.getTheme();
    }

    public void setTheme(String theme) {
        this.theme.setTheme(theme);
    }

    public String getDisplayTheme() {
        return theme.getDisplayTheme();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public void setPassword(char[] password) {
        if (password.length == 0) {
            this.passwordHashValue = "";
        } else {
            this.passwordHashValue = Arrays.toString(Hash.hashByMD5(getPasswordBytes(password)));
        }
        System.out.println("new passwordHashValue : " + passwordHashValue);
    }

    public boolean matchPassword(char[] password) {
        if (this.passwordHashValue == null
                || this.passwordHashValue.equals("")) {
            return password.length == 0;
        }
        byte[] passwordBytes = getPasswordBytes(password);
        return Arrays.toString(Hash.hashByMD5(passwordBytes)).equals(this.passwordHashValue);
    }

    public String getPasswordHashValue() {
        return passwordHashValue;
    }

    public void setPasswordHashValue(String passwordHashValue) {
        this.passwordHashValue = passwordHashValue;
    }

    private byte[] getPasswordBytes(char[] password) {
        return String.valueOf(password).getBytes();
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Task getTaskById(String id) {
        for (Task t : tasks) {
            if (t.getId().equals(id.trim())) {
                return t;
            }
        }
        return null;
    }
}
